<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>cpstun</title>
    <link rel="stylesheet" type="text/css" href="csound.css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.78.1" />
    <link rel="home" href="index.html" title="The Canonical Csound Reference Manual" />
    <link rel="up" href="OpcodesTop.html" title="Orchestra Opcodes and Operators" />
    <link rel="prev" href="cpstmid.html" title="cpstmid" />
    <link rel="next" href="cpstuni.html" title="cpstuni" />
  </head>
  <body>
    <div class="navheader">
      <table width="100%" summary="Navigation header">
        <tr>
          <th colspan="3" align="center">cpstun</th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="cpstmid.html">Prev</a> </td>
          <th width="60%" align="center">Orchestra Opcodes and Operators</th>
          <td width="20%" align="right"> <a accesskey="n" href="cpstuni.html">Next</a></td>
        </tr>
      </table>
      <hr />
    </div>
    <div class="refentry">
      <a id="cpstun"></a>
      <div class="titlepage"></div>
      <a id="IndexCpstun" class="indexterm"></a>
      <div class="refnamediv">
        <h2>
          <span class="refentrytitle">cpstun</span>
        </h2>
        <p>cpstun — 
      Returns micro-tuning values at k-rate.
    </p>
      </div>
      <div class="refsect1">
        <a id="idp96666400"></a>
        <h2>Description</h2>
        <p>
      Returns micro-tuning values at k-rate.
    </p>
      </div>
      <div class="refsect1">
        <a id="idp96707712"></a>
        <h2>Syntax</h2>
        <pre class="synopsis">kcps <span class="command"><strong>cpstun</strong></span> ktrig, kindex, kfn</pre>
      </div>
      <div class="refsect1">
        <a id="idp96709952"></a>
        <h2>Performance</h2>
        <p>
      <span class="emphasis"><em>kcps</em></span> -- Return value in cycles per second.
    </p>
        <p>
      <span class="emphasis"><em>ktrig</em></span> -- A trigger signal used to trigger the evaluation.
    </p>
        <p>
      <span class="emphasis"><em>kindex</em></span> -- An integer number denoting an index of scale.
    </p>
        <p>
      <span class="emphasis"><em>kfn</em></span> --  Function table containing the parameters (numgrades, interval, basefreq, basekeymidi) and the tuning ratios.
    </p>
        <p>
      These opcodes are similar to <a class="link" href="cpstmid.html" title="cpstmid"><em class="citetitle">cpstmid</em></a>, but work without necessity of MIDI.
    </p>
        <p>
      <span class="emphasis"><em>cpstun</em></span> works at k-rate. It allows fully customized micro-tuning scales. It requires a function table number containing the tuning ratios, and some other parameters stored in the function table itself.
    </p>
        <p>
      <span class="emphasis"><em>kindex</em></span> arguments should be filled with integer numbers expressing the grade of given scale to be converted in cps. In <span class="emphasis"><em>cpstun</em></span>, a new value is evaluated only when <span class="emphasis"><em>ktrig</em></span> contains a non-zero value.  The function table <span class="emphasis"><em>kfn</em></span> should be generated by <a class="link" href="GEN02.html" title="GEN02"><em class="citetitle">GEN02</em></a> and the first four values stored in this function are parameters that express:
      </p>
        <div class="itemizedlist">
          <ul class="itemizedlist" style="list-style-type: disc; ">
            <li class="listitem">
              <p>numgrades -- The number of grades of the micro-tuning scale.</p>
            </li>
            <li class="listitem">
              <p>interval -- The frequency range covered before repeating the grade ratios, for example 2 for one octave, 1.5 for a fifth etcetera.</p>
            </li>
            <li class="listitem">
              <p>basefreq -- The base frequency of the scale in cycles per second.</p>
            </li>
            <li class="listitem">
              <p>basekey -- The integer index of the scale to which to assign basefreq unmodified.</p>
            </li>
          </ul>
        </div>
        <p>
    </p>
        <p>
      After these four values, the user can begin to insert the tuning ratios.  For example, for a standard 12-grade scale with the base-frequency of 261 cps assigned to the key-number 60, the corresponding f-statement in the score to generate the table should be:

      </p>
        <div class="informalexample">
          <pre class="programlisting">
<span class="comment">;           numgrades    basefreq     tuning-ratios (eq.temp) .......</span>
<span class="comment">;                  interval    basekey</span>
<span class="stamnt">f</span>1 0 64 -2  12     2     261   60     1   1.059463 1.12246 1.18920 ..etc...</pre>
        </div>
        <p>
    </p>
        <p>
Another example with a 24-grade scale with a base frequency of 440 assigned
to the key-number 48, and a repetition interval of 1.5:

      </p>
        <div class="informalexample">
          <pre class="programlisting">
<span class="comment">;                  numgrades       basefreq      tuning-ratios .......</span>
<span class="comment">;                          interval       basekey</span>
<span class="stamnt">f</span>1 0 64 -2         24      1.5     440    48     1   1.01  1.02  1.03   ..etc...</pre>
        </div>
        <p>
    </p>
      </div>
      <div class="refsect1">
        <a id="idp96731904"></a>
        <h2>Examples</h2>
        <p>
      Here is an example of the cpstun opcode. It uses the file <a class="ulink" href="examples/cpstun.csd" target="_top"><em class="citetitle">cpstun.csd</em></a>.

      </p>
        <div class="example">
          <a id="idp96733760"></a>
          <p class="title">
            <strong>Example 153. Example of the cpstun opcode.</strong>
          </p>
          <div class="example-contents">
            <p>See the sections <a class="link" href="UsingRealTime.html" title="Real-Time Audio"><em class="citetitle">Real-time Audio</em></a> and <a class="link" href="CommandFlags.html" title="Csound command line"><em class="citetitle">Command Line Flags</em></a> for more information on using command line flags.</p>
            <pre class="programlisting">
<span class="csdtag">&lt;CsoundSynthesizer&gt;</span>
<span class="csdtag">&lt;CsOptions&gt;</span>
<span class="comment">; Select audio/midi flags here according to platform</span>
<span class="comment">; Audio out   Audio in</span>
-odac           -iadc    <span class="comment">;;;RT audio I/O</span>
<span class="comment">; For Non-realtime ouput leave only the line below:</span>
<span class="comment">; -o cpstun.wav -W ;;; for file output any platform</span>
<span class="csdtag">&lt;/CsOptions&gt;</span>
<span class="csdtag">&lt;CsInstruments&gt;</span>

<span class="comment">; Initialize the global variables.</span>
<span class="ohdr">sr</span> <span class="op">=</span> 44100
<span class="ohdr">kr</span> <span class="op">=</span> 4410
<span class="ohdr">ksmps</span> <span class="op">=</span> 10
<span class="ohdr">nchnls</span> <span class="op">=</span> 1

<span class="comment">; Table #1, a normal 12-tone equal temperament scale.</span>
<span class="comment">; numgrades = 12 (twelve tones)</span>
<span class="comment">; interval = 2 (one octave)</span>
<span class="comment">; basefreq = 261.659 (Middle C)</span>
<span class="comment">; basekeymidi = 60 (Middle C)</span>
gitemp <span class="ohdr">ftgen</span> 1, 0, 64, <span class="op">-</span>2, 12, 2, 261.659, 60, 1.00, \
             1.059, 1.122, 1.189, 1.260, 1.335, 1.414, \
             1.498, 1.588, 1.682, 1.782, 1.888, 2.000

<span class="comment">; Instrument #1.</span>
<span class="oblock">instr</span> 1
  <span class="comment">; Set the trigger.</span>
  ktrig <span class="opc">init</span> 1

  <span class="comment">; Use Table #1.</span>
  kfn <span class="opc">init</span> 1

  <span class="comment">; If the base key (note #60) is C, then 9 notes </span>
  <span class="comment">; above it (note #60 + 9 = note #69) should be A.</span>
  kindex <span class="opc">init</span> 69

  k1 <span class="opc">cpstun</span> ktrig, kindex, kfn

  <span class="opc">printk2</span> k1
<span class="oblock">endin</span>


<span class="csdtag">&lt;/CsInstruments&gt;</span>
<span class="csdtag">&lt;CsScore&gt;</span>

<span class="comment">; Play Instrument #1 for one second.</span>
<span class="stamnt">i</span> 1 0 1
<span class="stamnt">e</span>


<span class="csdtag">&lt;/CsScore&gt;</span>
<span class="csdtag">&lt;/CsoundSynthesizer&gt;</span>
</pre>
          </div>
        </div>
        <p><br class="example-break" />

      Its output should include lines like this:
      </p>
        <pre class="screen">i1   440.11044</pre>
        <p>
    </p>
      </div>
      <div class="refsect1">
        <a id="idp96738816"></a>
        <h2>See Also</h2>
        <p>
      <a class="link" href="cpstmid.html" title="cpstmid"><em class="citetitle">cpstmid</em></a>,
      <a class="link" href="cpstuni.html" title="cpstuni"><em class="citetitle">cpstuni</em></a>,
      <a class="link" href="GEN02.html" title="GEN02"><em class="citetitle">GEN02</em></a>
    </p>
      </div>
      <div class="refsect1">
        <a id="idp96742880"></a>
        <h2>Credits</h2>
        <p>Example written by Kevin Conder.</p>
      </div>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="cpstmid.html">Prev</a> </td>
          <td width="20%" align="center">
            <a accesskey="u" href="OpcodesTop.html">Up</a>
          </td>
          <td width="40%" align="right"> <a accesskey="n" href="cpstuni.html">Next</a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">cpstmid </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">Home</a>
          </td>
          <td width="40%" align="right" valign="top"> cpstuni</td>
        </tr>
      </table>
    </div>
  </body>
</html>
